import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class Demo05 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setSize(500,500);
        Ball ball = new Ball();
        frame.add(ball);
        frame.addWindowListener(ball);
        new Thread(ball).start();
        frame.setTitle("安梓嘉专属小球");
        frame.setVisible(true);
    }
}
class Ball extends Panel implements Runnable, WindowListener {

  int[]  x = new int[500];
   int[] y = new int[500];
    public Ball(){
       for (int i = 0; i < 500; i++){
           x[i] = (int)(Math.random()*500);
           y[i] = (int)(Math.random()*500);
       }
    }
    @Override
    public void paint(Graphics g) {
    for (int i = 0; i < 500; i++){
        g.setColor(new Color((int)Math.floor(Math.random()*255),(int)Math.floor(Math.random()*255),(int)Math.floor(Math.random()*255)));
        g.drawString("*",x[i],y[i]);
    }


    }

    @Override
    public void run() {
        while (true){
            for (int i = 0; i < 500; i++){
                y[i]++;
                if (y[i]>=500){
                    y[i]=0;
                }
                repaint();
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}
